package com.smcdd.cdb.dbo.cdb_dbo.ui.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smcdd.cdb.dbo.cdb_dbo.R;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BaseActivity;
import com.smcdd.cdb.dbo.cdb_dbo.ui.base.BasePresenter;
import com.smcdd.cdb.dbo.cdb_dbo.ui.utils.DialogUtils;

import cn.bingoogolapple.qrcode.core.QRCodeView;
import cn.bingoogolapple.qrcode.zxing.ZXingView;

/**
 * 作者：Administrator on 2017/8/10 19:22
 * 邮箱：zhanghuaiha@gmail.com
 */

public class CodeActivity extends BaseActivity implements QRCodeView.Delegate{

    private ZXingView zbarview;
    private TextView title,input,open,tv;
    private String small_prompt;
    private int send_factory;
    private LinearLayout bottom;
    private boolean flag = true;
    private ImageView back;

    @Override
    public void initView() {
        zbarview = (ZXingView) findViewById(R.id.zbarview);
        title = (TextView) findViewById(R.id.add_title);
        input = (TextView) findViewById(R.id.input);
        open = (TextView) findViewById(R.id.open);
        tv = (TextView) findViewById(R.id.tv);
        bottom = (LinearLayout) findViewById(R.id.bottom);
        back = (ImageView) findViewById(R.id.back);
        title.setText("扫描二维码");
        InitUI();
    }

    private void InitUI() {

        zbarview.setDelegate(this);
        zbarview.startCamera();
        zbarview.showScanRect();
        zbarview.startSpot();
    }

    @Override
    public void initData() {
        small_prompt = getIntent().getStringExtra("small_prompt");
        send_factory = getIntent().getIntExtra("send_factory", 0);
        if (small_prompt != null && !small_prompt.equals("")) {
            tv.setText(small_prompt);
        }else {
            bottom.setVisibility(View.GONE);
        }

        if (send_factory == 0) {
            input.setVisibility(View.GONE);
        }
        //开启关闭闪光灯
        open.setOnClickListener(v -> {
            if (flag) {
                zbarview.openFlashlight();
            }else {
                zbarview.closeFlashlight();
            }
            flag = !flag;
        });

        input.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.putExtra("result", "input");
            intent.putExtra("send_factory", send_factory);
            setResult(RESULT_OK,intent);
            finish();
        });

        back.setOnClickListener(v -> finish());
    }

    @Override
    protected void onStart() {
        super.onStart();
        zbarview.startCamera();
    }

    @Override
    protected void onStop() {
        zbarview.stopCamera();
        super.onStop();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (zbarview != null) {
            InitUI();
        }
    }


    @Override
    protected void onDestroy() {
        zbarview.onDestroy();
        super.onDestroy();
    }

    @Override
    public void onScanQRCodeSuccess(String result) {
        try {
            MediaPlayer mp = MediaPlayer.create(CodeActivity.this, R.raw.wx);
            mp.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (!TextUtils.isEmpty(result)) {
            Log.e("result", result);
            Intent intent = new Intent();
            intent.putExtra("result", result);
            intent.putExtra("send_factory", send_factory);
            setResult(RESULT_OK,intent);
            finish();
        } else {
            Toast.makeText(CodeActivity.this, "识别二维码失败！", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onScanQRCodeOpenCameraError() {
        DialogUtils.TSDialog(this,"相机权限未开启，无法使用此功能",true);
       // Toast.makeText(CodeActivity.this, "相机权限未开启", Toast.LENGTH_SHORT).show();
    }

//    private void dialog1(){
//        AlertDialog.Builder builder=new AlertDialog.Builder(this);  //先得到构造器
//        builder.setTitle("提示"); //设置标题
//        builder.setMessage("相机权限未开启，无法使用此功能"); //设置内容
//        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { //设置确定按钮
//            @Override
//            public void onClick(DialogInterface dialog, int which) {
//                finish();
//                dialog.dismiss(); //关闭dialog
//            }
//        });
//        //参数都设置完成了，创建并显示出来
//        builder.create().show();
//    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_code;
    }
}
